//
//  StoryDataManager.swift
//  LeTu
//
//  Created by 高宇 on 2024/4/29.
//

import Foundation

class StoryDataManager {
    
    static let shard = StoryDataManager()
    private(set) var scenicModel: ScenicModel?
    /// 分类信息
    var classifyItems = [StoryTypeModel]()
    /// 景区id
    private(set) var scenicId = 0
    /// 数据分页
    private var pageNum = 1
    /// 数据长度
    private(set) var pageSize = 20
    
    func setScenicId(scenicModel: ScenicModel) {
        self.scenicId = scenicModel.id
        self.classifyItems = scenicModel.storyTypeList
    }
    
    func reserManager() {
        scenicModel = nil
        classifyItems = [StoryTypeModel]()
        scenicId = 0
    }
    
    // MARK: - 获取讲解数据
    func getScenics(isLoad: Bool, currentClassifysIndex: Int, completeHandle: @escaping NetworkRequestHandle) {
        
        guard currentClassifysIndex < classifyItems.count else { return }
        let classifyModel = classifyItems[currentClassifysIndex]
        
        if classifyModel.isLoadAllSuccess == true {
            
            var dialog = DialogMessage()
            dialog.code = 200
            dialog.data = classifyItems[currentClassifysIndex]
            completeHandle(dialog)
            return
        }
        
        var pageNum = self.pageNum
        pageNum = isLoad ? pageNum + 1 : 1
        
        var param: [String : Any] = [String : Any]()
        param["page"] = pageNum
        param["pageSize"] = pageSize
        param["id"] = scenicId
        param["storyType"] = currentClassifysIndex+1
        
        apiProvider.request(api: .getScenicStorys(params: param)) { [weak self] json, jsonArr, code in
            MYLog("获取讲解数据：", json)
                        
            var dialog = DialogMessage()

            guard let strongSelf = self, let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]], classifyModel.isLoadAllSuccess == false else { return completeHandle(dialog) }
            
            strongSelf.pageNum = pageNum
            let tempList = Mapper<ScenicStoryModel>().mapArray(JSONArray: dataDic)
            dialog.data = tempList
            let classifyItem = strongSelf.classifyItems[currentClassifysIndex]
            if isLoad {
                classifyItem.sourceArr = classifyItem.sourceArr + tempList
            } else {
                classifyItem.sourceArr = tempList
            }
            strongSelf.classifyItems[currentClassifysIndex] = classifyItem
            
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
    func getAllScenics(scenicId: Int, currentClassifysIndex: Int, completeHandle: @escaping NetworkRequestHandle) {
                
        guard currentClassifysIndex < classifyItems.count else { return }
        let classifyModel = classifyItems[currentClassifysIndex]
        
        var param: [String : Any] = [String : Any]()
        param["page"] = 1
        param["pageSize"] = 150
        param["id"] = scenicId
        param["storyType"] = currentClassifysIndex+1
        
        classifyModel.isLoadAllSuccess = false
        apiProvider.request(api: .getScenicStorys(params: param)) { json, jsonArr, code in
            MYLog("获取全部讲解数据：", json)
                        
            var dialog = DialogMessage()

            guard let response = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = response.code
            dialog.message = response.msg
            guard dialog.code == 200, let dataDic = response.data as? [[String: Any]] else { return completeHandle(dialog) }
            let scenices = Mapper<ScenicStoryModel>().mapArray(JSONArray: dataDic)
            dialog.data = currentClassifysIndex
            
            DispatchQueue.main.async {
                            
                if currentClassifysIndex < StoryDataManager.shard.classifyItems.count {
                    // 设置展示数据
                    let classifyItem = StoryDataManager.shard.classifyItems[currentClassifysIndex]
                    classifyItem.sourceArr = scenices
                    classifyItem.isLoadAllSuccess = true
                    StoryDataManager.shard.classifyItems[currentClassifysIndex] = classifyItem
                }
                
            }
            
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
        
    }
    
}

